﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OneSoft.EntityCore;
using OneSoft.DataAccess;
using System.Data;

using BySoft.Server.Common.Entity;
using BySoft.Server.Common.Schema;
using BySoft.Server.Consts;

namespace BySoft.Server.DataAccess
{
    public partial class SysUseroperationlogDA
    {
        public static DataTable GetOperationLogList(string moduleID, string operatorInfo, string startDate,
                string endDate, string actionType, string theKey, int pageIndex, int pageSize, out int count)
        {
            #region SQL Server
            DataAccessParameterCollection parameters = new DataAccessParameterCollection();

            string sSql = "SELECT UserOperationLogID, UserID, UserName, TheKey, ActionType, ActionDescription, ActionResult, LogTime, IPAddress, ErrorCode, ExceptionMsg, DataXML, ModuleID, ModuleName FROM SYS_UserOperationLog WHERE 1=1 ";


            if (!String.IsNullOrEmpty(moduleID))
            {
                sSql += " AND " + SysUseroperationlogTable.C_MODULEID + "=@moduleID";
                parameters.AddWithValue("@moduleID", moduleID);
            }
            if (!String.IsNullOrEmpty(operatorInfo))
            {
                sSql += " AND (Lower(" + SysUseroperationlogTable.C_USERID + ") like @oper";
                parameters.AddWithValue("@oper", "%" + operatorInfo.ToLower() + "%");
                sSql += " OR Lower(" + SysUseroperationlogTable.C_USERNAME + ") like @opername )";
                parameters.AddWithValue("@opername", "%" + operatorInfo.ToLower() + "%");
            }
            if (!String.IsNullOrEmpty(startDate))
            {
                sSql += " AND " + SysUseroperationlogTable.C_LOGTIME + " >=@StartDate";
                parameters.AddWithValue("@StartDate", Convert.ToDateTime(startDate));
            }
            if (!String.IsNullOrEmpty(endDate))
            {
                sSql += " AND " + SysUseroperationlogTable.C_LOGTIME + " <=@EndDate";
                parameters.AddWithValue("@EndDate", Convert.ToDateTime(endDate + " 23:59:59"));
            }
            if (!String.IsNullOrEmpty(actionType))
            {
                sSql += " AND " + SysUseroperationlogTable.C_ACTIONTYPE + "=@actionType";
                parameters.AddWithValue("@actionType", actionType);
            }
            if (!String.IsNullOrEmpty(theKey))
            {
                sSql += " AND Lower(" + SysUseroperationlogTable.C_THEKEY + ") like @thekey";
                parameters.AddWithValue("@thekey", "%" + theKey.ToLower() + "%");
            }
            sSql += " ORDER BY " + SysUseroperationlogTable.C_LOGTIME + " DESC ";

//            DataTable dtq = new DataTable();
//            count = 0;

//            string sql = string.Format("select count(1) as rowscount from ({0}) t ", sSql);
//            string sqlq = string.Format(@"IF OBJECT_ID('tempdb..#Temp') IS NOT NULL
//   DROP TABLE #Temp
//   IF OBJECT_ID('tempdb..#TempOrder') IS NOT NULL
//   DROP TABLE #TempOrder
//select a.* INTO #TempOrder
//from ({0}) a
//SELECT identity(int,1,1) as [ROW_NUMBER],a.* INTO #Temp FROM #TempOrder a"+ " ORDER BY a." + SysUseroperationlogTable.C_LOGTIME + " DESC "+
//"SELECT * FROM #Temp where a.ROW_NUMBER BETWEEN "+pageIndex * pageSize + 1+" and "+(pageIndex + 1) * pageSize, sSql);
            
            
//            using(DataAccessBroker broker=DataAccessFactory.Instance())
//            {
//                try
//                {
//                    broker.BeginTransaction();
//                    count = Convert.ToInt32(broker.FillSQLDataSet(sql).Tables[0].Rows[0][0].ToString());
//                    dtq = broker.FillSQLDataSet(sqlq).Tables[0]; 
//                    broker.Commit();
                   
                  
//                }
//                catch
//                {
//                    broker.Rollback();
//                }
//            } 
//            return dtq;
           // return DataAccessCommon.ExecuteDataTablePagingFor2000(sSql, pageIndex, pageSize, parameters, out count,SysUseroperationlogTable.C_LOGTIME + " DESC ");
            return DataAccessCommon.ExecuteDataTablePaging(sSql, pageIndex, pageSize, parameters, out count);
            #endregion
        }
    }
}
